package com.example.administrator.capital.mine;


import android.app.Fragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.capital.R;
import com.example.administrator.capital.adapter.callback.OnPopWinDisMisBack;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.eventbus.EventBase;
import com.example.administrator.capital.eventbus.EventCardBean;
import com.example.administrator.capital.mine.second.BindCardFragment;
import com.example.administrator.capital.mine.second.SetDealPasswordFragment;
import com.example.administrator.capital.model.bean.BankList;
import com.example.administrator.capital.model.bean.GetMoneyBean;
import com.example.administrator.capital.presenter.MinePresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.DisPlayUtils;
import com.example.administrator.capital.utils.JsonUtils;
import com.example.administrator.capital.utils.PreferenceManager;
import com.example.administrator.capital.utils.ToastUtil;
import com.example.administrator.capital.widget.CommonDialog;
import com.example.administrator.capital.widget.CustomPopupWindow;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import eventbusactivityscope.EventBusActivityScope;

/**
 * A simple {@link Fragment} subclass.
 * 提现
 */
public class GetMoneyFragment extends BaseBackFragment {
    @BindView(R.id.iv_bank_icon)
    ImageView mIvBankIcon;
    @BindView(R.id.tv_bank_name)
    TextView mTvBankName;
    @BindView(R.id.tv_bank_number)
    TextView mTvBankNumber;
    @BindView(R.id.tv_point)
    TextView mTvPoint;
    @BindView(R.id.tv_balance)
    TextView mTvBalance;
    @BindView(R.id.btn_bind)
    Button mBtnBind;
    Unbinder unbinder;
    @BindView(R.id.rl_bank)
    RelativeLayout mRlBank;
    @BindView(R.id.tv_cash_all)
    TextView mTvCashAll;
    @BindView(R.id.et_point)
    EditText etPoint;
    @BindView(R.id.layout)
    LinearLayout layout;
    private TextView mBack;
    private MinePresenter minePresenter;
    private CustomPopupWindow mCustomPopupWindow;
    private String bankid;
    private int uid;
    public GetMoneyFragment() {
        // Required empty public constructor
    }
    public static GetMoneyFragment newInstance(String money) {

        Bundle args = new Bundle();
        args.putString("money",money);
        GetMoneyFragment fragment = new GetMoneyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_get_money, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        EventBusActivityScope.getDefault(_mActivity).register(this);
        mBack = view.findViewById(R.id.app_title_back);
        minePresenter = new MinePresenter(null);
    }
    @Override
    protected void setListener() {
        super.setListener();
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();
            }
        });
        mRlBank.setOnClickListener(this);
        mTvCashAll.setOnClickListener(this);
        mBtnBind.setOnClickListener(this);
    }
    @Override
    protected void setData() {
        super.setData();
        mTvBalance.setText(mBundle.getString("money")+"元");
        tixian();
    }
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.rl_bank:
                start(CardFragment.newInstance());
                break;
            case R.id.tv_cash_all:
                etPoint.setText(mBundle.getString("money"));
                break;
            case R.id.btn_bind:
                if (uid==0) {
                    start(SetDealPasswordFragment.newInstance());
                }else {
                    if (TextUtils.isEmpty(etPoint.getText().toString())) {
                        ToastUtil.showShort("金额不能为空");
                    } else {
                        lend();
                    }
                }
                break;
        }
    }
    private void tixian() {
        minePresenter.withdrawals(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                ToastUtil.showShort(var);
            }

            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
            }

            @Override
            public void onSuccess(Response<String> data) {
                GetMoneyBean bean = JsonUtils.parse(data.body(), GetMoneyBean.class);
                if (bean.getData().getData1().size() == 0) {

                } else {
                   mTvBankName.setText(bean.getData().getData1().get(0).getAccountname());
                    mTvBankNumber.setText(bean.getData().getData1().get(0).getBankaccount());
                    bankid = String.valueOf(bean.getData().getData1().get(0).getId());
                }
            }

            @Override
            public void onFail(Response<String> error) {
            }
        }, PreferenceManager.instance().getUseId());
        minePresenter.banklist(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                ToastUtil.showShort(var);
            }
            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
            }
            @Override
            public void onSuccess(Response<String> data) {
                BankList bean = JsonUtils.parse(data.body(), BankList.class);
                uid = bean.getData().getUid();
            }
            @Override
            public void onFail(Response<String> error) {
            }
        });
    }

    private void applywithdrawals(String uid, String Cash, String bankselect, String paypass) {
        minePresenter.applywithdrawals(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                ToastUtil.showShort(var);
            }

            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
            }

            @Override
            public void onSuccess(Response<String> data) {
                ToastUtil.showShort("提现成功");
                EventBusActivityScope.getDefault(getActivity()).post(new EventBase("刷新界面"));
                mCustomPopupWindow.dismissWindow();
                _mActivity.onBackPressed();
            }

            @Override
            public void onFail(Response<String> error) {
            }
        }, uid, Cash, bankselect, paypass);
    }


    private void lend() {
        mCustomPopupWindow = new CustomPopupWindow(getActivity().getWindow(),getContext(), R.layout.pay_money,
                DisPlayUtils.getWidthPx(), DisPlayUtils.getHeightPx(), new OnPopWinDisMisBack() {
            @Override
            public void onPopWindowDismiss() {

            }
        }) {
            @Override
            public void setData(View view) {
                ImageView back_img = view.findViewById(R.id.iv_back);

                back_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dismissWindow();
                    }
                });
                final EditText editText = view.findViewById(R.id.password);
                Button button = view.findViewById(R.id.sure);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(editText.getText().toString())) {
                            ToastUtil.showShort("请输入交易密码");
                        } else {
                            applywithdrawals(PreferenceManager.instance().getUseId(), etPoint.getText().toString(), bankid, editText.getText().toString());
                        }

                    }
                });


            }
        };
        mCustomPopupWindow.showAsDownWindow(layout);
    }
    @Subscribe
    public void onTabSelectedEvent(EventCardBean event) {
        mTvBankName.setText(event.getName());
        mTvBankNumber.setText(event.getNum());
        bankid = event.getId();
    }

    @Subscribe
    public void onTabSelectedEvent(EventBase event) {
        if (event.getName().equals("交易密码")) {
            tixian();
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
